<?php
/***
**自我游系统对接
**date 2015-10-09 
**user by pzp
***/
class ZiWoYou{
	public function __construct($aid){
		switch ($aid){
			case 113:
				$this->custId='291009';
				$this->apikey='E885A0E22E64EB8D38CC7338407E997B';
				$this->url ='http://u.dgkangfei.com/api/';
			break;			
			
			case 56540: //易票天下
				$this->custId='278789';
				$this->apikey='2A5CE1C582488D2E23D25966A2FB8156';
				$this->url ='http://b2b.ykghgj.com/api/';
			break;
			
			case 35033: //金飞马
				$this->custId='298409';
				$this->apikey='0075C13294133D0C008642ED04D32ADD';
				$this->url ='http://www.jfmtraveling.com/api/';
			break;
	
			default:
			break; 
		}
	}
	
	//产品列表
	public function ProductList(){ 
		$method = 'list.jsp';
		$requet = array(
			'custId'	=> $this->custId,
			'apikey'	=> $this->apikey,
			'keyWrod'	=> '',
			'treeId'	=> 1,
			'cityName'	=> '',
			'isPay'		=> '',
			'isConfirm'	=> '',
			'isExpress'	=> '',
			'viewId'	=> '',
			'tagIds'	=> '',
			'location'	=> '',
			'pageNum'	=> '',
			'pageNo'	=> '',
			'orderBy'	=> '',
			'isPackage'	=> '',
		);
		$zwy_response = $this->ziwoyou_post($requet,$method);
		print_r($zwy_response);
	}
	
	//产品详细
	public function ProductDetail(){
		$method = 'detail.jsp';
		$requet = array(
			'custId'	=> $this->custId,
			'apikey'	=> $this->apikey,
			'productNo'	=> '7915130'
		);//7919145,7915130
		$zwy_response = $this->ziwoyou_post($requet,$method);
		print_r($zwy_response);
	}
	
	//获取产品状态
	public function GetProductState(){
		$method = 'getProductState.jsp';
		$requet = array(
			'custId'	=> $this->custId,
			'apikey'	=> $this->apikey,
			'productNo'	=> '7915130'
		);//7919145,7915130
		$zwy_response = $this->ziwoyou_post($requet,$method);
		print_r($zwy_response);
	}
	
	
	//产品日历价格获取
	public function ProductPrice(){
		$method = 'price.jsp';
		$requet = array(
			'custId'	=> $this->custId,
			'apikey'	=> $this->apikey,
			'productNo'	=> '7915130',
			'travelDate'=>	'2015-10-09',
			'endTravelDate'	=> ''			
		);
		$zwy_response = $this->ziwoyou_post($requet,$method);
		print_r($zwy_response);
	}
	
	
	//订单提交保存
	public function Order($array){
		$method = 'order.jsp';
		$param = <<<xml
			<order>
				<travel_date>{$array['travel_date']}</travel_date>
				<end_travel_date></end_travel_date>
				<arrived_time></arrived_time>
				<info_id>{$array['info_id']}</info_id>
				<cust_id>{$this->custId}</cust_id>
				<get_type></get_type>
				<order_source_id>{$array['order_source_id']}</order_source_id>
				<order_memo></order_memo>
				<num>{$array['num']}</num>
				<link_man>{$array['link_man']}</link_man>
				<link_phone>{$array['link_phone']}</link_phone>
				<link_email></link_email>
				<link_address></link_address>
				<linkCode></linkCode>
				<link_credit_type>0</link_credit_type>
				<link_credit_no>{$array['link_credit_no']}</link_credit_no>
				<peoples>
					<link_man>{$array['link_man']}</link_man>
					<link_credit_type>0</link_credit_type>
					<link_credit_no>{$array['link_credit_no']}</link_credit_no>
				</peoples>
				<conds>
				</conds>
			</order>
xml;
		// print_r($param);
		$requet = array(
			'custId'	=> $this->custId,
			'apikey'	=> $this->apikey,
			'param'		=> $param
		);
		$zwy_response = $this->ziwoyou_post($requet,$method);
		// print_r($zwy_response);exit;
		if((int)$zwy_response->status == 1){
			$this->logfile($array['order_source_id'].'|'.(string)$zwy_response->order_id,'Order_s');
			return (string)$zwy_response->order_id;
		}
		else{
			$this->logfile(json_encode($array).'|'.(int)$zwy_response->error_state.'|'.(string)$zwy_response->msg,'Order_f');
			return 101;
		}
	}
	
	
	//订单支付
	public function OrderPay($orderId){
		$method = 'pay.jsp';
		$requet = array(
			'custId'	=> $this->custId,
			'apikey'	=> $this->apikey,
			'orderId'	=> $orderId
		);
		$zwy_response = $this->ziwoyou_post($requet,$method);
		if((int)$zwy_response->status == 1){
			$this->logfile($orderId.'|'.(string)$zwy_response->orderId.'|'.(string)$zwy_response->code,'OrderPay_s');
			return $zwy_response;
		}
		else{
			$this->logfile($orderId.'|'.(int)$zwy_response->error_state.'|'.(string)$zwy_response->msg,'OrderPay_f');
			return 101;
		}
		// print_r($zwy_response);
	}
	
	//订单取消(未支付/已支付)
	public function OrderCancel($orderId){
		$method = 'cancelOrder.jsp';		
		$requet = array(
			'custId'	=> $this->custId,
			'apikey'	=> $this->apikey,
			'orderId'	=> $orderId
		);
		$zwy_response = $this->ziwoyou_post($requet,$method);
		if($zwy_response->status == 1){
			$this->logfile($orderId.'|'.(string)$zwy_response->status,'cancelOrder_s');
			return 100;
		}else{
			$this->logfile($orderId.'|'.(string)$zwy_response->status.'|'.(string)$zwy_response->error_state.'|'.(string)$zwy_response->msg,'cancelOrder_f');
			return 101;
		}
	}
	
	//订单修改(已支付)
	public function OrderRefund($orderId,$num){
		$method = 'refundOrder.jsp';
		$requet = array(
			'custId'	=> $this->custId,
			'apikey'	=> $this->apikey,
			'orderId'	=> $orderId,
			'num'		=> $num
		);
		$zwy_response = $this->ziwoyou_post($requet,$method);
		if($zwy_response->status == 1){
			$this->logfile($orderId.'|'.$num.'|'.(string)$zwy_response->status,'refundOrder_s');
			return 100;
		}else{
			$this->logfile($orderId.'|'.$num.'|'.(string)$zwy_response->status.'|'.(string)$zwy_response->error_state.'|'.(string)$zwy_response->msg,'refundOrder_f');
			return 101;
		}
		// print_r($zwy_response);
	}
	
	//订单详情
	public function OrderDetail(){
		$method = 'orderDetail.jsp';
		$requet = array(
			'custId'	=> $this->custId,
			'apikey'	=> $this->apikey,
			'orderId'	=> '4909782'
		);
		$zwy_response = $this->ziwoyou_post($requet,$method);
		print_r($zwy_response);
	}
	
	//获取订单状态
	public function getOrderState($orderId){
		$method = 'getOrderState.jsp';
		$requet = array(
			'custId'	=> $this->custId,
			'apikey'	=> $this->apikey,
			'orderId'	=> $orderId
		);
		$zwy_response = $this->ziwoyou_post($requet,$method);
		print_r($zwy_response);
	}
	
	//提交数据
	public function ziwoyou_post($requet,$method){
		$url=$this->url.$method;
		$o=""; 
		foreach ($requet as $k=>$v){ 
			$o.= "$k=".urlencode($v)."&"; 
		} 
		$post_data = substr($o,0,-1); 
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 0); 
		curl_setopt($ch, CURLOPT_HEADER, 0); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL,$url); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
		$reponse = curl_exec($ch);
		curl_close($ch);
		$reponse = simplexml_load_string( $reponse, 'SimpleXMLElement', LIBXML_NOCDATA );
		return $reponse;
	}
	
	
	//日志记录
	// public function logfile($txt, $logname){
        // $fp = fopen('/var/www/html/new/d/logs/api/ZiWoYou/'.$logname.date('Y-m-d').'.txt',"a");
        // flock($fp, LOCK_EX);
        // fwrite($fp, date('Y-m-d H:i:s').'|'.$txt . "\n");
        // flock($fp, LOCK_UN);
        // fclose($fp);
    // }
	
	public function logfile($word,$path){
		$filepath= '/var/www/html/new/d/logs/api/ZiWoYou/'.$path.'.txt';
		if(file_exists($filepath)){
			$word = date("Y-m-d H:i:s")."|".$word;
			// $word = mb_convert_encoding($word, 'GBK', 'UTF-8');
			$maxsize  = 1024*1024*10;
			$filesize = filesize($filepath);
			$filetype = end(explode(".",$filepath));
			$filename = substr($filepath,0,strpos($filepath,'.'));
			if($filesize>$maxsize)
			{
				rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
				$handler=fopen($filepath,'w');
				flock($handler, LOCK_EX) ;
				fwrite($handler,"\n".$word);
				flock($handler, LOCK_UN);
				fclose($handler);
			}
			else
			{
				$fp = fopen('/var/www/html/new/d/logs/api/ZiWoYou/'.$path.'.txt',"a");
				flock($fp, LOCK_EX);
				fwrite($fp,$word."\n");
				flock($fp, LOCK_UN);
				fclose($fp);
			}
		}else{
			$fp = fopen('/var/www/html/new/d/logs/api/ZiWoYou/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}
}

// $zwy = new ZiWoYou();
// $ProductList = $zwy->ProductList();
// $ProductDetail = $zwy->ProductDetail();
// $GetProductState = $zwy->GetProductState();
// $ProductPrice = $zwy->ProductPrice();
// $array = array(
	// 'travel_date'	=> '2015-10-09',
	// 'info_id'		=> '7915130',
	// 'order_source_id'=> '20151009pzp001',
	// 'num'			=> 2,
	// 'link_man'		=> 'pzp',
	// 'link_phone'	=> '15959110796',
	// 'link_credit_no'=> '350321199108230123',
// );
// $Order = $zwy->Order($array);
// $OrderDetail = $zwy->OrderDetail();
// $OrderPay = $zwy->OrderPay('4909782');
// $OrderCancel = $zwy->OrderCancel('4909782');
// $OrderRefund = $zwy->OrderRefund('4909782',1);
// $getOrderState = $zwy->getOrderState('4909782');
?>